
<template>
  <div class="employee-edit-container">
    <el-card title="编辑员工" class="card-container">
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
        style="max-width: 800px; margin: 0 auto;"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入员工姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职位" prop="position">
              <el-input v-model="form.position" placeholder="请输入职位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="部门" prop="department">
              <el-select v-model="form.department" placeholder="请选择部门">
                <el-option v-for="dept in departments" :key="dept.value" :label="dept.label" :value="dept.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" type="email" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入职日期" prop="hireDate">
              <el-date-picker
                v-model="form.hireDate"
                type="date"
                placeholder="选择日期"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item style="text-align: center; margin-top: 30px;">
          <el-button type="primary" :loading="loading" @click="submitForm">
            保存
          </el-button>
          <el-button @click="resetForm" style="margin-left: 10px;">
            重置
          </el-button>
          <el-button @click="router.go(-1)" style="margin-left: 10px;">
            返回
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElForm } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const form = reactive({
  id: '',
  name: '',
  position: '',
  department: '',
  email: '',
  phone: '',
  hireDate: ''
})

const rules = {
  name: [
    { required: true, message: '请输入员工姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  position: [
    { required: true, message: '请输入职位', trigger: 'blur' }
  ],
  department: [
    { required: true, message: '请选择部门', trigger: 'change' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ],
  hireDate: [
    { required: true, message: '请选择入职日期', trigger: 'change' }
  ]
}

const formRef = ref(null)
const loading = ref(false)
const employeeId = route.params.id

const departments = [
  { label: '技术部', value: '技术部' },
  { label: '产品部', value: '产品部' },
  { label: '设计部', value: '设计部' },
  { label: '测试部', value: '测试部' },
  { label: '市场部', value: '市场部' },
  { label: '人力资源部', value: '人力资源部' }
]

// 模拟员工数据
const mockEmployees = [
  { id: 1, name: '张三', position: '前端开发工程师', department: '技术部', email: 'zhangsan@example.com', phone: '13800138000', hireDate: '2020-01-15' },
  { id: 2, name: '李四', position: '后端开发工程师', department: '技术部', email: 'lisi@example.com', phone: '13900139000', hireDate: '2020-03-20' },
  { id: 3, name: '王五', position: '产品经理', department: '产品部', email: 'wangwu@example.com', phone: '13700137000', hireDate: '2019-11-10' },
  { id: 4, name: '赵六', position: 'UI设计师', department: '设计部', email: 'zhaoliu@example.com', phone: '13600136000', hireDate: '2021-05-05' },
  { id: 5, name: '钱七', position: '测试工程师', department: '测试部', email: 'qianqi@example.com', phone: '13500135000', hireDate: '2020-07-30' }
]

onMounted(() => {
  fetchEmployeeDetails()
})

const fetchEmployeeDetails = async () => {
  loading.value = true
  try {
    // 实际项目中替换为真实API
    // const response = await axios.get(`/api/employees/${employeeId}`)
    // form = response.data
    const employee = mockEmployees.find(emp => emp.id === Number(employeeId))
    if (employee) {
      Object.assign(form, employee)
    } else {
      ElMessage.warning('未找到员工信息')
      router.push({ name: 'EmployeeList' })
    }
  } catch (error) {
    console.error('Error fetching employee details:', error)
    ElMessage.error('加载员工详情失败')
    router.push({ name: 'EmployeeList' })
  } finally {
    loading.value = false
  }
}

const submitForm = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    loading.value = true

    // 实际项目中替换为真实API
    // await axios.put(`/api/employees/${employeeId}`, form)
    console.log('更新员工数据:', form)
    ElMessage.success('员工信息更新成功')
    router.push({ name: 'EmployeeList' })
  } catch (error) {
    if (error.name === 'ValidationError') return
    console.error('Error updating employee:', error)
    ElMessage.error('员工信息更新失败')
  } finally {
    loading.value = false
  }
}

const resetForm = () => {
  fetchEmployeeDetails()
}
</script>

<style scoped>
.card-container {
  max-width: 900px;
  margin: 0 auto;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}
</style>